package com.gxa.saas.controller;

import com.gxa.saas.pojo.Position;
import com.gxa.saas.service.PositionService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;


import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 权限的操作
 * @author wanghai
 * @date 2022/6/7 15:30
 */
@RestController
@RequestMapping("/position")
@CrossOrigin
public class PositionController {
    @Autowired
    PositionService positionService;

    @ApiOperation("分页查询的操作")
    @PostMapping("/selectByPage")
    public Map selectByPage(@RequestParam("page") Integer page){
        List<Position> positions = positionService.selectByPage(page);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(positions);
        return jsonResult.getMap();
    }

    @ApiOperation("添加职位的操作")
    @PostMapping("/add")
    public Map addPosition(@RequestBody Position position){
        positionService.addPosition(position);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }
    @ApiOperation("更新职位信息的操作")
    @PostMapping("/update")
    public Map updatePosition(@RequestBody Position position){
        positionService.updatePosition(position);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }
    @ApiOperation("删除职位的操作")
    @PostMapping("/delete")
    public Map deletePosition(@RequestParam("id") Integer id){
        positionService.deletePosition(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }
    @ApiOperation("更新职位启用状态的操作")
    @PostMapping("/updateIsUsed")
    public Map updateIsUsed(@RequestBody Map map){
        positionService.updateIsUsed(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }
    @ApiOperation("获取职位的总数")
    @PostMapping("/getPositionList")
    public Map selectAll(){
        Integer positions = positionService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(positions);
        return jsonResult.getMap();
    }

    @ApiOperation("获取当前职位所拥有的权限")
    @PostMapping("/getRole")
    public Map getRole(@RequestBody Position position){
        List<Integer> roles = positionService.getRole(position);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(roles);
        return jsonResult.getMap();
    }

    @ApiOperation("设置权限的操作")
    @PostMapping("/setRole")
    public Map setRole(@RequestBody Map map){
        positionService.setRole(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getMap();
    }
}
